//
//  AppDelegate.m
//  BSBDJ
//
//  Created by user on 16/5/23.
//  Copyright © 2016年 Burning. All rights reserved.
//

#import "AppDelegate.h"
#import "TZTabbarController.h"
#import "TZCoverView.h"
#import "VersionTool.h"
#import "UMSocial.h"
#import "UMSocialSinaSSOHandler.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"
#import "TZAdController.h"
@interface AppDelegate ()
@property (nonatomic, weak) TZCoverView *cover;
@property (nonatomic, assign) BOOL firstRun;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation AppDelegate

- (TZCoverView *)cover{
    if (_cover == nil) {
        TZCoverView *cover = [TZCoverView coverView];
        cover.frame = [UIApplication sharedApplication].keyWindow.bounds;
        
        _cover = cover;
    }
    return _cover;
}

/*5746fa3667e58e1914002df5*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    // 设置根控制器
    self.window.rootViewController = [[TZAdController alloc] init];
    
    // 显示窗口
    [self.window makeKeyAndVisible];
    
    if ((_firstRun = [VersionTool compareCurrentVersionAndSystemVersion])){
        self.cover.cancleBlock = ^{
            [_cover removeFromSuperview];
         
        };
        
        self.cover.DefaultBlock = ^{
            [_cover removeFromSuperview];
        };
        
        [[UIApplication sharedApplication].keyWindow addSubview:self.cover];
    }
    
    //-------集成 分享功能-----//
    //设置友盟社会化组件appkey
    [UMSocialData setAppKey:@"5746fa3667e58e1914002df5"];
    //设置微信AppId、appSecret，分享url
    [UMSocialWechatHandler setWXAppId:@"wxd930ea5d5a258f4f" appSecret:@"db426a9829e4b49a0dcac7b4162da6b6" url:@"http://www.umeng.com/social"];
    //设置手机QQ 的AppId，Appkey，和分享URL，需要#import "UMSocialQQHandler.h"
    [UMSocialQQHandler setQQWithAppId:@"100424468" appKey:@"c7394704798a158208a74ab60104f0ba" url:@"http://www.umeng.com/social"];
    //打开新浪微博的SSO开关，设置新浪微博回调地址，这里必须要和你在新浪微博后台设置的回调地址一致。需要 #import "UMSocialSinaSSOHandler.h"
    [UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:@"2126715115"
                                              secret:@"    a2094366d167719829f8867c924af757"
                                         RedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application
{
    // 前台 按下home键
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // 进入后台后
    [self stopTimer];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // 从后台进入程序时
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // 进入程序后
    [self startTimer];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    [self clearMemory];
}

- (void)startTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(clearMemory) userInfo:nil repeats:true];
    
}
- (void)stopTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

- (void)clearMemory
{
    [[SDImageCache sharedImageCache] clearMemory];
}


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    BOOL result = [UMSocialSnsService handleOpenURL:url];
    if (result == FALSE) {
        //调用其他SDK，例如支付宝SDK等
    }
    return result;
}


@end
